<?php require_once("Audio.class.php");

/**
 *  
 */

class AudioIndex
{
    private $_db;               # PDO Object
    private $_resultSet;        # PDO Resultset
    private $_objects;          # Audio objects
    private $_html;             # Output HTML
    
    /**
     * @access public
     */
    public function __construct(&$link, &$main)
    {
        var_dump($main->getLog());
        if(is_object($link)) $this->_db = $link;
        try 
        {
	        $sql = 'SELECT * FROM media_audio';
	    	$query = $this->_db->query($sql);
	    	foreach($query as $row)
	    	{
	    	    $this->_objects[] = new Audio($row);
	    	}
    	}
    	catch (PDOException $exception)
    	{
    	       
    		die($exception->getMessage());
    	}
    
    }
    
    private function getTable()
    {
        $html  = '<table class="itemTable">';
        $html .= '    <tr>';
        $html .= '        <th>ID:</th><th>Artist:</th>';
        $html .= '    </tr>';
        $i = 0;
        foreach($this->_objects as $object)
        {
            $color = ( $i % 2 == 0 ) ? 'itemTableColor1' : 'itemTableColor2';  
            $html .= sprintf("
            <tr class=\"$color\">
                <td>%s</td>
                <td>%s</td>
            </tr>
            ", $object->getId(), $object->getArtist());
            $i++;
        }
        $html .= '</table>';
        return $html;
    }
    
    /**
     * Converts data to html
     * @uses $this->_objects;
     */
    public function __toString()
    {
        return $this->getTable();
    }    
    
    public function getObjectById($id)
    {
        if(is_numeric($id))
        {
            return $this->_objects[$id];
        }
        else 
        {
            return false;
        }
    }
    
    /**
     * @access public
     */
    public function __destruct()
    {
        echo $this->getTable();
    }
}

/*$link = new PDO('mysql:host=localhost;port=3306;dbname=mediadevil', 'localuser', 'localpasswd');
$a = new AudioIndex($link);
var_dump($a);*/